﻿using System;
using System.Linq;
using Core.DataAccess.Model;
using Core.DataAccess.Model.Projects;
using Core.Framework.Redis.Queue_Helper;
using Core.IBusiness.IProjectModule;
using Core.Framework.Util;
using Core.Framework.Model.Common;
using System.Collections.Generic;
using System.Linq.Expressions;
using Core.Framework.Util.Common;

namespace Core.Business.ProjectModule
{
    [NotUseBaseHttpPipelineInfo]
    public class Project: IProject
    {
        

        public MethodResult<ProjectList> GetProjectInfo(int userKey, string projectToken)
        {
            try
            {
                using (var context = new ProjectContext())
                {

                    var data = context
                        .ProjectList
                        .Where(a => a.UserKey == userKey && a.Token == projectToken)
                        .FirstOrDefault();

                    return new MethodResult<ProjectList>{ Date = data, IsFinished = true};

                };
            }
            catch (Exception e)
            {
                return new MethodResult<ProjectList>{ Discription = e.Message, IsFinished = false};
            }
        }

        public MethodResult<ProjectList> GetProjectInfoAndPayInfoByScheme(string scheme)
        {
            try
            {
                var result = new MethodResult<ProjectList> { IsFinished = true };

                using (var context = new ProjectContext())
                {

                    result.Date = context
                        .ProjectList
                        .Where(a => a.ServicePath == scheme)
                        .FirstOrDefault();

                    if (result.Date == null)
                    {
                        result.IsFinished = false;
                        result.Discription = "相关信息不存在";
                        return result;
                    }


                    // 获取支付宝信息
                    result.Date.AliPayInfo = context
                        .ProjectAliPay
                        .Where(a => a.ProjectKey == result.Date.Id)
                        .FirstOrDefault();

                    // 获取微信信息
                    result.Date.WChatPayInfo = context
                        .ProjectWChatPay
                        .Where(a => a.ProjectKey == result.Date.Id)
                        .FirstOrDefault();
                }

                return result;
            }
            catch (Exception e)
            {
                return new MethodResult<ProjectList> { IsFinished = false, Discription = e.Message };
                throw;
            }

        }

        public MethodResult<ProjectList> IsExist(int userKey, string projectToken)
        {
            var info = this.GetProjectInfo(userKey,projectToken);

            if (!info.IsFinished)
            {
                return info;
            }

            if (info.Date == null && info.Date.Id < 1)
                return new MethodResult<ProjectList>{ Discription = "项目不存在", IsFinished = false};

           return new MethodResult<ProjectList>{ Date = info.Date, IsFinished = true};

        }


        public MethodResult<int> WChatPayInfoExec(ProjectWChatPay model, SqlExecType sqlExecType)
        {
            return Utils.SqlExecType<ProjectContext, ProjectWChatPay>(model,
                sqlExecType,
                a => a.Id == model.Id);
        }

        public MethodResult<int> AliPayInfoExec(ProjectAliPay model, SqlExecType sqlExecType)
        {
            return Utils.SqlExecType<ProjectContext, ProjectAliPay>(model,
                sqlExecType,
                a => a.Id == model.Id);
        }

        public MethodResult<ProjectAliPay> GetAliPayInfo(int projectKey)
        {
            try
            {
                using (var context = new ProjectContext())
                {
                    var data = context
                        .ProjectAliPay
                        .Where(a => a.ProjectKey == projectKey)
                        .FirstOrDefault();

                    return new MethodResult<ProjectAliPay> { Date = data, IsFinished = true};
                }
            }
            catch (Exception e)
            {
                return new MethodResult<ProjectAliPay> { Discription = e.Message, IsFinished = false};
            }
        }

        public MethodResult<ProjectWChatPay> GetWChatPayInfo(int projectKey)
        {
            try
            {
                using (var context = new ProjectContext())
                {
                    var data = context
                        .ProjectWChatPay
                        .Where(a => a.ProjectKey == projectKey)
                        .FirstOrDefault();

                    return new MethodResult<ProjectWChatPay> { Date = data, IsFinished = true };
                }
            }
            catch (Exception e)
            {
                return new MethodResult<ProjectWChatPay> { Discription = e.Message, IsFinished = false };
            }
        }

        public MethodResult<int> ProjectExec(ProjectList model, SqlExecType sqlExecType)
        {
            return Utils.SqlExecType<ProjectContext, ProjectList>(model,
                sqlExecType,
                a => a.Id == model.Id);
        }

        public MethodResult<List<ProjectList>> GetProjectList(int userKey, string search, Pagination pagination)
        {
            try
            {
                using (var context = new ProjectContext())
                {

                    Expression<Func<ProjectList, bool>> lamdba = (a) => a.UserKey == userKey;

                    if (!string.IsNullOrWhiteSpace(search))
                        lamdba = lamdba.And(a => a.Title.Contains(search));

                    var data = context
                        .ProjectList
                        .Where(lamdba)
                        .Pagination(pagination)
                        .ToList();

                    return new MethodResult<List<ProjectList>> { Date = data, IsFinished = true };

                };
            }
            catch (Exception e)
            {
                return new MethodResult<List<ProjectList>> { Discription = e.Message, IsFinished = false };
            }
        }
    }
}
